package org.jeecg.modules.zcgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.modules.zcgl.entity.AssetContractLog;
import org.jeecg.modules.zcgl.entity.LeaseContractInfo;
import org.jeecg.modules.zcgl.enums.YesNoEnum;
import org.jeecg.modules.zcgl.mapper.AssetContractLogMapper;
import org.jeecg.modules.zcgl.service.IAssetContractLogService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description: 资产签订合同记录信息
 * @Author: jeecg-boot
 * @Date:   2021-01-26
 * @Version: V1.0
 */
@Service
public class AssetContractLogServiceImpl extends ServiceImpl<AssetContractLogMapper, AssetContractLog> implements IAssetContractLogService {


    @Override
    @Transactional
    public boolean creatorLog(String assetId, String assetSubId,Date rentStartTime, LeaseContractInfo leaseContractInfo){
        AssetContractLog log = new AssetContractLog();
        log.setAssetId(assetId);
        log.setAssetSubId(assetSubId);
        log.setLeaseContractId(leaseContractInfo.getId());
        log.setRentStartTime(rentStartTime);
        log.setRentEndTime(leaseContractInfo.getRentEndTime());
        log.setCompanyId(leaseContractInfo.getCompanyId());
        log.setContractStatus(leaseContractInfo.getStatus());
        log.setMonthlyRent(leaseContractInfo.getMonthlyRent());
        log.setRentPrice(leaseContractInfo.getRentPrice());
        log.setIzEnd(YesNoEnum.NO.getStatus());
        return super.retBool(baseMapper.insert(log));
    }


    @Override
    @Transactional
    public boolean updateEnd(String assetId, String leaseContractId, String assetSubId){
        AssetContractLog log = new AssetContractLog();
        log.setAssetId(assetId);
        log.setLeaseContractId(leaseContractId);
        log.setAssetSubId(assetSubId);
        UpdateWrapper<AssetContractLog> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(AssetContractLog::getIzEnd, YesNoEnum.YES.getStatus());
        updateWrapper.setEntity(log);
        return super.update(updateWrapper);
//        AssetContractLog log = baseMapper.selectById(id);
//        if (log == null){
//            return false;
//        }
//        log.setIzEnd(YesNoEnum.NO.getStatus());
//        return super.retBool(baseMapper.updateById(log));
    }


    @Override
    public int countNum(String assetId, String leaseContractId, String assetSubId, Integer izEnd){
        AssetContractLog log = new AssetContractLog();
        log.setAssetId(assetId);
        log.setLeaseContractId(leaseContractId);
        log.setAssetSubId(assetSubId);
        log.setIzEnd(izEnd);
        QueryWrapper<AssetContractLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(log);
        return super.count(queryWrapper);
    }

    @Override
    public Date selectMaxRentEndTimeByKey(String assetId, String leaseContractId, String assetSubId, Integer izEnd) {
        AssetContractLog log = new AssetContractLog();
        log.setAssetId(assetId);
        log.setLeaseContractId(leaseContractId);
        log.setAssetSubId(assetSubId);
        log.setIzEnd(izEnd);
        QueryWrapper<AssetContractLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(log);
        return baseMapper.selectMaxRentEndTimeByKey(queryWrapper);
    }
}
